<!--
 * @Author: fengweicong 75202172+fengweicong@users.noreply.github.com
 * @Date: 2022-02-16 16:53:19
 * @LastEditors: fengweicong 75202172+fengweicong@users.noreply.github.com
 * @LastEditTime: 2022-07-17 08:38:20
 * @FilePath: \B2B\ebuy\src\views\Main\Management\Person\PersonInformation\Address\index.vue
 * @Description: 
 * 
 * Copyright (c) 2022 by fengweicong 75202172+fengweicong@users.noreply.github.com, All Rights Reserved. 
-->
<template>
  <div class="address">
    <div class="card1" style="margin-bottom:20px;">
      <el-card>
        <div slot="header" class="">
          <span>收货地址</span>
        </div>
        <el-form
          ref="form"
          :model="form"
          label-width="100px"
          label-position="left">
          <el-form-item label="收货人">
            <el-input
              v-model="form.receiver_name"
              placeholder="请填写收货人姓名"
            ></el-input>
          </el-form-item>
          <el-form-item label="地区">
            <el-cascader
              size="large"
              :options="region"
              v-model="selectedOptions"
              v-if="isShowAddressInfo"
            >
            </el-cascader>
          </el-form-item>
          <el-form-item label="手机号">
            <el-input v-model="form.receiver_phone" placeholder="请填写手机号"></el-input>
          </el-form-item>
          <el-form-item label="详细地址">
            <el-input
              type="textarea"
              :rows="4"
              placeholder="请填写详细地址"
              v-model="form.receiver_deatailed_address"
            >
            </el-input>
          </el-form-item>
          <el-form-item label="设为默认地址">
            <el-switch v-model="form.is_default_address"></el-switch>
          </el-form-item>
          <el-button
            type="primary"
            style="margin-left: 30%; width: 40%; border-radius: 6px"
            @click="addAddress" v-if="isAdd"
            >保存</el-button
          >
          <el-button
            type="primary"
            style="margin-left: 30%; width: 40%; border-radius: 6px"
            @click="modifyAddress" v-else
            >修改</el-button
          >
        </el-form>
      </el-card>
    </div>
  </div>
</template>
<script>
import { AreaData, AreaCodeToText, AreaTextToCode } from '@/utils/utils'
export default {
  data() {
    return {
      form: {
        receiver_name: "",
        receiver_phone:"",
        receiver_deatailed_address:"",
        is_default_address: false,
      },
      
      region: AreaData(),
      selectedOptions: [],
      isShowAddressInfo:true,

      list:[{
         
      }],
      pid: '',//买家id
      address_id: '',
      isAdd: true, //默认是增加
    };
  },
  computed:{
    receiver_area() {
      return AreaCodeToText(this.selectedOptions)
    }
  },
  methods: {
    async addAddress() {
      if (this.form.is_default_address) {
        await this.$API.getDeliveryAddress().then(res => {
          if (res.status == 200) {
            let list = []
            for (let obj of res.data) {
              if (obj.is_default_address) {
                obj.is_default_address = false
                list.push(this.$API.modifyDeliveryAddress(obj, obj.id))
              }
            }
            return Promise.all(list)
          }
        }).catch(error => {
          console.log(error)
          this.$message.error('出错啦')
          return
        })
      }
      this.$API.addDeliveryAddress({
        ...this.form,
        "purchaser_id": this.pid,
        "receiver_area": this.receiver_area,
      }).then(res => {
        if (res.status == 201) {
          this.$message.success('添加成功')
          this.$router.go(-1)
        }
      })
    },
    async modifyAddress() {
      if (this.form.is_default_address) {
        await this.$API.getDeliveryAddress().then(res => {
          if (res.status == 200) {
            let list = []
            for (let obj of res.data) {
              if (obj.is_default_address) {
                obj.is_default_address = false
                list.push(this.$API.modifyDeliveryAddress(obj, obj.id))
              }
            }
            return Promise.all(list)
          }
        }).catch(error => {
          console.log(error)
          this.$message.error('出错啦')
          return
        })
      }
      this.$API.modifyDeliveryAddress({
        ...this.form,
        "purchaser_id": this.pid,
        "receiver_area": this.receiver_area,
      },this.address_id).then(res => {
        if (res.status == 200) {
          this.$message.success('修改成功')
          this.$router.go(-1)
        }
      })
    },
    async load() {
      if (this.$store.state.params.aid) {
        this.isAdd = false
        let aid = this.$store.state.params.aid
        await this.$API.getDeliveryAddress().then(res => {
          if (res.status == 200) {
            for (let obj of res.data) {
              if (obj.id == aid) {
                this.form = {
                  'receiver_name':obj.receiver_name,
                  'receiver_phone': obj.receiver_phone,
                  'receiver_deatailed_address': obj.receiver_deatailed_address,
                  'is_default_address': obj.is_default_address
                }
                this.address_id = obj.id
              }
              this.selectedOptions = AreaTextToCode(obj.receiver_area)
            }
          }
      })
      }
      await this.$API.getPurchaser().then(res => {
        if (res.status == 200) {
          let aid = this.$store.state.user.account_id
          for (let obj of res.data) {
            if (obj.account_id == aid) this.pid = obj.id
          }
         }
      })
    },
  },
  created() {
    this.load()
  }
};
</script>
<style scoped>

</style>